<template>
	<form-container
		:title="formData.id ? '编辑模块' : '增加模块'"
		:width="700"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="库位编号：" name="reservoircode">
				<a-input v-model:value="formData.reservoircode" placeholder="请输入仓库编号" allow-clear />
			</a-form-item>
      <a-form-item label="库位名称：" name="reservoirname">
				<a-input v-model:value="formData.reservoirname" placeholder="请输入仓库名称" allow-clear />
			</a-form-item>
      <a-form-item label="库位面积" name="area">
				<a-input v-model:value="formData.area" placeholder="请输入面积" allow-clear />
			</a-form-item>
      <a-form-item label="仓库：" name="warehouseid">
        <a-select v-model:value="formData.warehouseid" placeholder="请选择分类" :options="categoryOptions" allow-clear>
        </a-select>
			</a-form-item>
		</a-form>
		<template #footer>
			<a-checkbox class="mr-2" v-if="!formData.id" v-model:checked="addNext">继续新增下一条</a-checkbox>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit">保存</a-button>
		</template>
	</form-container>
</template>

<script setup>
import { required } from '@/utils/formRules'
import reservoirapi from '@/api/wms/reservoir/reservoirapi'
import tool from '@/utils/tool'

	// 默认是关闭状态
	let visible = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()

	// 表单数据
	const formData = ref({})
	// 继续新增下一条
	const addNext = ref(false)

	// 打开抽屉
	const onOpen = (record) => {
		visible.value = true
		addNext.value = false
		formData.value = {
			sortCode: 99
		}
		if (record) {
			formData.value = Object.assign({}, record)
		}
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		visible.value = false
	}





	// 默认要校验的
	const formRules = {
		reservoircode: [required('请输入库位编号')],
		reservoirname: [required('请输入库位名称')],
    area: [required('请输入库位面积')],
    warehouseid: [required('请选择仓库')],
	}

	// 验证并提交数据
	const onSubmit = () => {
		formRef.value.validate().then(() => {
			reservoirapi.Add(formData.value).then(() => {
				if (addNext.value) {
					formData.value = {
						sortCode: 99
					}
				} else {
					onClose()
				}
				emit('successful')
			})
		})
	}
   // 分类字典获取仓库
   const categoryOptions = tool.dictList('warehouseid')

	// 调用这个函数将子组件的一些数据和方法暴露出去
	defineExpose({
		onOpen
	})

</script>
